package com.qf.bbapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.bbapi.dao.TCoincertificateDao;
import com.qf.bbapi.dao.TExchangeIntegralDao;
import com.qf.bbapi.dao.TIntegralDao;
import com.qf.bbapi.entity.TCoincertificate;
import com.qf.bbapi.entity.TExchangeIntegral;
import com.qf.bbapi.entity.TIntegral;
import com.qf.bbapi.service.intf.TExchangeIntegralService;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ````':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 *
 * @author 贾
 * @version 1.0
 * @description: TODO
 * @date 2022/7/2 22:15
 */
@Service
public class TExchangeIntegralImpl implements TExchangeIntegralService {
    @Resource
    private TExchangeIntegralDao dao;
    @Resource
    private TCoincertificateDao coincertificateDao;
    @Resource
    private TIntegralDao integralDao;
    @Override
@Transactional
    public R exchange(TExchangeIntegral integral, Integer token) {
        if (integral != null && token > 0) {
            QueryWrapper<TIntegral> wrapper = new QueryWrapper<TIntegral>().eq("uid", token);
            TIntegral tIntegral = integralDao.selectOne(wrapper);
            System.out.println(tIntegral);
            if (tIntegral != null) {
                if (tIntegral.getIntegral() >= integral.getExchange()) {
                    if (integralDao.updateByUid(tIntegral.getIntegral() - integral.getExchange(),token) > 0) {
                        QueryWrapper<TCoincertificate> wrapper1 = new QueryWrapper<>();
                        QueryWrapper<TCoincertificate> uid = wrapper1.eq("uid", token);
                        TCoincertificate selectOne = coincertificateDao.selectOne(uid);
                        if(selectOne == null){
                            if(coincertificateDao.insert(new TCoincertificate(token,0,0,0,0))>0){
                                TCoincertificate uid1 = coincertificateDao.selectOne(new QueryWrapper<TCoincertificate>().eq("uid", token));
                                if(integral.getType().equals("mtso")){
                                    if(coincertificateDao.updatemtso(uid1.getMtso()+1,token)>0){
                                        TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                         if(uid2!=null) {
                                                if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                    return R.ok("兑换成功");
                                                }
                                        }
                                    }

                                }else if(integral.getType().equals("gift_certificates")){
                                    if(coincertificateDao.updategiftCertificates(uid1.getGiftCertificates()+1,token)>0){
                                        TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                        if(uid2!=null) {
                                                if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                    return R.ok("兑换成功");
                                            }
                                        }
                                    }
                                }else if(integral.getType().equals("gift_coupons")){
                                   if(coincertificateDao.updategiftCoupons(uid1.getGiftCoupons()+1,token)>0){
                                       TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                       if(uid2!=null) {

                                               if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                   return R.ok("兑换成功");
                                               }
                                       }
                                   }
                                }else if(integral.getType().equals("dayticket")){
                                    if(coincertificateDao.updatedayticket(uid1.getDayticket()+1,token)>0){
                                        TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                        if(uid2!=null) {
                                                if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                    return R.ok("兑换成功");
                                                }
                                        }
                                    }
                                }
                            }

                        }else {
                            TCoincertificate uid1 = coincertificateDao.selectOne(new QueryWrapper<TCoincertificate>().eq("uid", token));
                            if(integral.getType().equals("mtso")){
                                if(coincertificateDao.updatemtso(selectOne.getMtso()+1,token)>0){
                                    TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                    if(uid2!=null) {
                                            if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                return R.ok("兑换成功");
                                            }
                                    }
                                }

                            }else if(integral.getType().equals("gift_certificates")){
                                if(coincertificateDao.updategiftCertificates(selectOne.getGiftCertificates()+1,token)>0){
                                    TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                    if(uid2!=null) {
                                            if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                return R.ok("兑换成功");
                                            }
                                    }
                                }
                            }else if(integral.getType().equals("gift_coupons")){
                                if(coincertificateDao.updategiftCoupons(selectOne.getGiftCoupons()+1,token)>0){
                                    TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                    if(uid2!=null) {
                                            if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                return R.ok("兑换成功");
                                        }
                                    }
                                }
                            }else if(integral.getType().equals("dayticket")){
                                if(coincertificateDao.updatedayticket(selectOne.getDayticket()+1,token)>0){
                                    TIntegral uid2 = integralDao.selectOne(new QueryWrapper<TIntegral>().eq("uid", token));
                                    if(uid2!=null) {
                                            if (dao.insert(new TExchangeIntegral(token, integral.getType(), integral.getExchange())) > 0) {
                                                return R.ok("兑换成功");
                                            }
                                    }
                                }
                            }
                        }
                    }
                }else {
                    return R.fail("积分不够");
                }
            }

        }
        return R.fail();
    }
}
